package net.example.lesson4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class TestLoopList {

	static final int N = 50000;
	
	static long foreach(List<String> list) {
		long start = System.currentTimeMillis();
		for (String j : list) {
			System.out.print(j);
		}
		return System.currentTimeMillis() - start;
	}
	
	static long iterator (List<String> list) {
		long start = System.currentTimeMillis();
		Iterator<String> iterator = list.iterator();
		while (iterator.hasNext()) {
			iterator.next();
		}
		return System.currentTimeMillis() - start;
	}
	
	static long indexFor(List<String> list) {
		long start = System.currentTimeMillis();
		for (int j = 0; j < list.size(); j++) {
			list.get(j);
		}
		return System.currentTimeMillis() - start;
	}
	
	static long addTailList(List<String> list) {
		long start = System.currentTimeMillis();
 		for (int i = 0; i < N; i++) {
			list.add(String.valueOf(i));
		}
		return System.currentTimeMillis() - start;
	}
	
	public static void main(String[] args) {
		final List<String> arrayList = new ArrayList<String>();
		final List<String> linkedList = new LinkedList<String>(); 
		
		System.out.println("ArrayList 末尾添加 " + N + "条耗时：" + addTailList(arrayList));
		System.out.println("LinkedList 末尾添加添加 " + N + "条耗时：" + addTailList(linkedList));
		System.out.println("==========================================");
		
		
		System.out.println("ArrayList foreach " + N + "条耗时：" + foreach(arrayList));
		System.out.println("LinkedList foreach " + N + "条耗时：" + foreach(linkedList));
		System.out.println("==========================================");
		 
		System.out.println("ArrayList iterator " + N + "条耗时：" + iterator(arrayList));
		System.out.println("LinkedList iterator " + N + "条耗时：" + iterator(linkedList));
		System.out.println("==========================================");
		 
		System.out.println("ArrayList indexFor " + N + "条耗时：" + indexFor(arrayList));
		System.out.println("LinkedList indexFor " + N + "条耗时：" + indexFor(linkedList));
		 

	}
	
	
}
